import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../resources.dart';

/// created by hcl
/// on 2024/02/23
/// page LoginTextField
/// desc 登录输入框
class NormalTextField extends StatelessWidget {
  TextEditingController textController;
  String hintText;
  String? title;
  String? unit;

  // TextStyle? style;
  // TextStyle? hintStyle;
  int maxLines;
  int maxLength;
  bool enable;
  bool showPassword;
  bool textAlignEnd;
  TextInputType keyboardType;
  List<TextInputFormatter>? inputFormatters;
  TextInputAction textInputAction;
  Function? unitTap;

  NormalTextField(
    this.textController,
    this.hintText, {
    super.key,
    this.title,
    this.unit,
    // this.hintStyle,
    this.maxLines = 1,
    this.maxLength = 40,
    this.enable = true,
    this.showPassword = false,
    this.textAlignEnd = false,
    this.keyboardType = TextInputType.text,
    this.inputFormatters,
    this.textInputAction = TextInputAction.done,
    this.unitTap,
  });

  FocusNode focusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 48.ss,
      padding: EdgeInsets.only(left: 5.ss, right: 5.ss),
      decoration: BoxDecoration(
          color: ColorUtils.color_FFFFFF,
          border: Border.all(color: ColorUtils.color_666666),
          borderRadius: BorderRadius.circular(5.ss)),
      child: Row(
        children: [
          title != null ? Text(title!, style: normal318) : SizedBox(),
          Expanded(child: _buildTextField()),
          unit != null ? InkWell(onTap: (){
            if(unitTap!=null){
              unitTap!();
            }
          },child: Text(unit!, style: normal318)) : SizedBox(),
        ],
      ),
    );
  }

  Widget _buildTextField() {
    return TextField(
      maxLines: maxLines,
      maxLength: maxLength,
      controller: textController,
      focusNode: focusNode,
      enabled: enable,
      keyboardType: keyboardType,
      obscureText: showPassword,
      textInputAction: textInputAction,
      // 数字、手机号限制格式为0到9(白名单)， 密码限制不包含汉字（黑名单）
      inputFormatters: getInputFormatter(),
      style:  normal318,
      textAlign: textAlignEnd?TextAlign.end:TextAlign.start,
      decoration: InputDecoration(
        counterText: '',
        hintText: hintText,
        hintStyle: normalC18,
        contentPadding: const EdgeInsets.all(0),
        filled: true,
        fillColor: ColorUtils.color_FFFFFF,
        border: _getBorder(),
        enabledBorder: _getBorder(),
        disabledBorder: _getBorder(),
        focusedBorder: _getBorder(),
      ),
    );
  }

  List<TextInputFormatter>? getInputFormatter() {
    if (inputFormatters != null) {
      return inputFormatters!;
    } else {
      List<TextInputFormatter> tempList = [];
      if (keyboardType == TextInputType.number ||
          keyboardType == TextInputType.phone) {
        tempList.add(FilteringTextInputFormatter.digitsOnly);
      }

      ///如果展示密码
      // if (needPassword) {
      //   tempList
      //       .add(FilteringTextInputFormatter.allow(RegExp("[\u4e00-\u9fa5]")));
      // }
    }
  }

  OutlineInputBorder _getBorder() {
    return const OutlineInputBorder(
      borderSide: BorderSide(
        color: Colors.transparent,
      ),
    );
  }
}
